﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WPF入门
{
    public partial class Form4 : Form
    {
        // 年，月变量
        private int year;
        private int month;
        private int day;

        public Form4()
        {
            InitializeComponent();
            // 初始化年月变量为当前日期
            year = DateTime.Now.Year;
            month = DateTime.Now.Month;
            day = DateTime.Now.Day;
        }

        /// <summary>
        /// 窗体加载事件，初始化月份和年份选择框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form4_Load(object sender, EventArgs e)
        {

            // 获取一年12个月的所有月份
            var months = System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames;
            foreach (var month in months)
            {
                if (!string.IsNullOrEmpty(month))
                    uiComboBox1.Items.Add(month);
            }
            // 设置默认选中项为当前月份
            uiComboBox1.SelectedIndex = DateTime.Now.Month - 1;

            // 设置uiCalendar1的初始日期为当前日期
            uiCalendar1.Date = DateTime.Now;

            // 获取当前年份，以及上下5年的范围
            int currentYear = DateTime.Now.Year;
            int startYear = currentYear - 5;
            int endYear = currentYear + 5;
            // 创建一个列表来存储年份
            List<int> years = new List<int>();
            // 将年份添加到列表中
            for (int year = startYear; year <= endYear; year++)
            {
                years.Add(year);
            }
            // 将年份列表绑定到uiComboBox2
            uiComboBox2.DataSource = years;
            // 设置默认选中项为当前年份
            uiComboBox2.SelectedItem = currentYear;

            // 设置uiTextBox1的初始内容为根据year, month, day格式化的日期字符串
            uiTextBox1.Text = $"{year}-{month:D2}-{day:D2}"; // 使用D2格式化为两位数



        }

        /// <summary>
        /// 设置月份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiComboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            // 获取uiComboBox1的选中值
            string selectedMonth = uiComboBox1.SelectedItem?.ToString();
            // 修改uiCalendar1的显示月份
            if (!string.IsNullOrEmpty(selectedMonth))
            {
                // 获取选中月份的索引
                int monthIndex = uiComboBox1.SelectedIndex + 1; // 索引从0开始，月份从1开始
                if (monthIndex > 0)
                {
                    month = monthIndex;
                }
                // 设置uiCalendar1的显示月份
                uiCalendar1.Date = new DateTime(year, month, day);

                // 更新uiTextBox1的内容
                uiTextBox1.Text = $"{year}-{month:D2}-{day:D2}"; // 使用D2格式化为两位数
            }
        }

        /// <summary>
        /// 设置年份
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiComboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            // 获取uiComboBox2的选中值
            int selectedYear = (int)uiComboBox2.SelectedItem;
            // 修改uiCalendar1的显示年份
            if (selectedYear > 0)
            {
                // 获取当前月份
                year = selectedYear;
                // 设置uiCalendar1的显示年份
                uiCalendar1.Date = new DateTime(year, month, day);

                // 更新uiTextBox1的内容
                uiTextBox1.Text = $"{year}-{month:D2}-{day:D2}"; // 使用D2格式化为两位数
            }
        }
        /// <summary>
        /// 处理日期变化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiCalendar1_OnDateTimeChanged(object sender, Sunny.UI.UIDateTimeArgs e)
        {
            // 时间变化，更新uitextBox1的内容
            if (e.DateTime != null)
            {
                // 获取选中的日期
                DateTime selectedDate = e.DateTime;
                // 更新uiTextBox1的内容
                uiTextBox1.Text = selectedDate.ToString("yyyy-MM-dd");
                // 更新年月日变量
                year = selectedDate.Year;
                month = selectedDate.Month;
                day = selectedDate.Day;
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            // 用消息框显示当前选中的日期，消息框弹出的位置为当前窗体的中心位置
            MessageBox.Show(this, $"当前选中的日期是：{uiTextBox1.Text}", "日期选择", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void uiGroupBox2_Load(object sender, EventArgs e)
        {
            // 设置uiAnalogMeter1的初始值为当前时间的小时数
            uiAnalogMeter1.Value = DateTime.Now.Hour;
            // 设置uihscrollBar1的最大和最小值
            uiHorScrollBar1.Maximum = 0;
            uiHorScrollBar1.Maximum = 100; // 小时范围从0到23
            // 设置uiHorScrollBar1更容易看见
            uiHorScrollBar1.BackColor = Color.Black;
            // 设置uiHorScrollBar1的滑块颜色为红色
            uiHorScrollBar1.ForeColor = Color.Red;

        }

        private void uiGroupBox2_Click(object sender, EventArgs e)
        {

        }

        private void uiHorScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            // 当uiHorScrollBar1的值变化时，更新uiAnalogMeter1的值
            uiAnalogMeter1.Value = uiHorScrollBar1.Value;
        }
    }
}
